//
//  HQNetWork.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/12.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQNetWork.h"

@implementation HQNetWork

/**
 网络请求
 
 @param param 参数
 @param suc 成功回调
 @param fail 失败回调
 */
+ (void)requetParam:(NSDictionary *)param withMethod:(NSString *)method suc:(void (^)(NSDictionary *))suc fail:(void (^)(void))fail{
    NSString *methodURL = [NSString stringWithFormat:@"%@%@",kServiceDomain,method];
    NSLog(@"请求方法名:%@\r 参数:%@",methodURL,param);
    [[self getManager] POST:methodURL parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"接口返回原始数据是: 【%@】",result);
        if(suc){
            NSLog(@"【%@】接口返回数据:",method);
            [self sucdata:[result objectFromJSONString]];
            suc([result objectFromJSONString]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self errorDescription:error];
        fail();
    }];
}

#pragma mark -----图片上传----
- (void)httpUploadImages:(UIImage *)image
                     URL:(NSString *)URLPath
                   param:(NSDictionary *)dict
                 suc:(void (^)(NSDictionary *))success fail:(void (^)(void))failure
{
    if (image == nil) {
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dict];
    NSString *fileNameStr = @"liaobu";
    URLPath = [NSString stringWithFormat:@"%@%@",kServiceDomain,URLPath];
    [self.httpRequestSessionManager POST:URLPath parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSData *imageData = UIImageJPEGRepresentation(image,0.4);
        
        [formData appendPartWithFileData:imageData
                                    name:@"file"
                                fileName:[NSString stringWithFormat:@"%@.jpg",fileNameStr]
                                mimeType:@"image/jpeg"];
        NSLog(@"图片信息--%@",formData);
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"上传进度==%f",uploadProgress.fractionCompleted);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"响应的-->responseObject:\n%@", responseObject);
        NSLog(@"\n==========HTTP(s)-Success============\n");
        NSLog(@"\n==========HTTP(s)-Success============\n");
//        NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
//        if(success){
            success(responseObject);
//        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure();
    }];
}

- (AFHTTPSessionManager *)httpRequestSessionManager{
    static  AFHTTPSessionManager *manager = nil;
    static dispatch_once_t singleManager;
    dispatch_once(&singleManager, ^{
        manager = [AFHTTPSessionManager manager];
        //        [manager setSecurityPolicy:[self securityPolicy]];
        AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
        [securityPolicy setAllowInvalidCertificates:YES];
        //这里进行设置；
        [manager setSecurityPolicy:securityPolicy];
        //        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        //         manager.responseSerializer = [AFJSONResponseSerializer serializer];
    }); 
    return manager;
}

/**
 获取网络请求实例
 
 @return ***
 */
+(AFHTTPSessionManager *)getManager{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    return manager;
}

/**
 取消网络请求
 */
+(void)cancelOperationQueue{
    [[AFHTTPSessionManager manager].operationQueue cancelAllOperations];
}

/**
 打印网络请求失败返回信息
 
 @param error 数据信息
 */
+(void)errorDescription:(NSError *)error{
    NSLog(@"网络请求错误信息: %@",error);
}

/**
 打印网络请求成功返回信息
 
 @param dic 数据信息
 */
+(void )sucdata:(NSDictionary *)dic{
    NSLog(@"网络请求返回信息: %@",dic);
}

@end
